import _axios from "./axios";

class SmrzApi {
  constructor() {
    this.hlwyyToken = ''
  }

  //直接这一步就行
  getMsbUser(code, backFn) {
    return new Promise(async resolve => {
      try {
        backFn("第一步：获取hlwyyToken")
        await this.getToken()
        backFn("第二步：通过蒙速办code获取加密参数")
        let res1 = await this.queryMsbTokenParams(code)
        backFn("第三步：通过加密参数获取蒙速办token")
        let res2 = await this.queryMsbToken(res1)
        backFn("第四步：通过蒙速办token获取加密参数")
        let res3 = await this.queryMsbUserParams(res2)
        backFn("第五步：通过加密参数获取蒙速办用户信息")
        let res4 = await this.queryMsbUser(res3)
        backFn("获取蒙速办用户信息成功！")
        resolve(res4)
      } catch (e) {
        uni.showToast({title: e})
      }
    })
  }

  getToken() {
    return new Promise(resolve => {
      let url = "http://mdej.impc.com.cn/hlwyy/business-ggfw/communal/getToken"
      _axios({url, method: 'get'}).then(res => {
        this.hlwyyToken = res.data.data
        resolve(res.data.data)
      })
    })
  }

  queryMsbTokenParams(code) {
    return new Promise(resolve => {
      let url = `http://msbyy.impc.com.cn/msb/hlwyy/business-ggfw/msbdlrz/queryMsbTokenParams?code=${code}`
      _axios({url, method: 'get', headers: {'hlwyy-Token': this.hlwyyToken}}).then(res => {
        resolve(res.data.data)
      })
    })
  }

  queryMsbUserParams(token) {
    return new Promise(resolve => {
      let url = `http://msbyy.impc.com.cn/msb/hlwyy/business-ggfw/msbdlrz/queryMsbUserParams?token=${token}`
      _axios({url, method: 'get', headers: {'hlwyy-Token': this.hlwyyToken}}).then(res => {
        resolve(res.data.data)
      })
    })
  }

  queryMsbToken(query) {
    let {app_id, code, sign, grant_type} = query
    app_id = encodeURIComponent(app_id), code = encodeURIComponent(code), sign = sign
    grant_type = encodeURIComponent(grant_type)
    let params = {app_id, code, sign, grant_type}
    let url = "https://app.zwfw.nmg.gov.cn:4443/icity/auth2.0/token"
    // let url = "http://120.133.52.68:4321/msbzwfw/icity/auth2.0/token"
    return new Promise((resolve, reject) => {
      _axios({
        url, method: 'post', form: params,
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
      }).then(res => {
        let result = res.data
        let {code, message} = result
        if (code == 1000) {
          let {code, data: {auth_token: {auth_token}}} = result
          return resolve(auth_token);
        } else reject(message)
      })
    })
  }

  queryMsbUser(query) {
    let {app_id, auth_token, sign} = query
    app_id = encodeURIComponent(app_id)
    auth_token = encodeURIComponent(auth_token)
    sign = encodeURIComponent(sign)
    // let url = `https://app.zwfw.nmg.gov.cn:4443/icity/auth2.0/get_user_info?app_id=${app_id}&auth_token=${auth_token}&sign=${sign}`
    let url = `http://120.133.52.68:4321/msbzwfw/icity/auth2.0/get_user_info?app_id=${app_id}&auth_token=${auth_token}&sign=${sign}`
    return new Promise((resolve, reject) => {
      _axios({
        url, method: 'get',
      }).then(res => {
        let result = res.data
        let {code, message} = result
        if (code == 1000) {
          let {code, data: {auth_info}} = result
          return resolve(auth_info);
        } else reject(message)
      })
    })
  }
}

export default new SmrzApi()
